package dcp.bss.modules.bussiness.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import dcp.bss.modules.bussiness.entity.ShipmentEntity;
import dcp.bss.modules.bussiness.service.ShipmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import dcp.bss.query.QueryViewVo;
import dcp.bss.web.util.ExcelUtil;
import dcp.bss.modules.bussiness.entity.EntpLocEntity;
import dcp.bss.modules.bussiness.service.EntpLocService;
import dcp.bss.common.util.PageUtils;
import dcp.bss.common.util.R;


/**
 * 围栏表
 *
 * @author caochaofeng
 * @email caochaofeng@dacanginfo.com
 * @date 2018-08-19 17:21:27
 */
@RestController
@RequestMapping("entploc")
@Slf4j
public class EntpLocController {
    @Autowired
    private EntpLocService entpLocService;
    @Autowired
    private ShipmentService shipmentService;
    /**
     * 列表
     */
    @RequestMapping("/page")
    // @RequiresPermissions("bussiness:entploc:list")
    public R list(QueryViewVo<EntpLocEntity> queryViewVo) {
        PageUtils page = entpLocService.queryPage(queryViewVo);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("bussiness:entploc:info")
    public R info(@PathVariable("id") Long id) {
        EntpLocEntity entpLoc = entpLocService.selectById(id);

        return R.ok().put("entpLoc", entpLoc);
    }



    /**
     * 查询某家企业的线路信息
     */
    @RequestMapping("/fence")
    // @RequiresPermissions("bussiness:lockstatus:info")
    public R fence(String vecNo) {
        ShipmentEntity shipmentEntity = shipmentService.selectOne(
                new EntityWrapper<ShipmentEntity>().eq("vec_no", vecNo));
        if(shipmentEntity!=null) {
            EntpLocEntity carrierLocEntity = entpLocService.selectOne(
                    new EntityWrapper<EntpLocEntity>().eq("entp_id", shipmentEntity.getCarrierId()).ne("type", 3));
            EntpLocEntity customerLocEntity = entpLocService.selectOne(
                    new EntityWrapper<EntpLocEntity>().eq("entp_id", shipmentEntity.getCustomerId()).ne("type", 3));

            EntpLocEntity loadLocEntity = entpLocService.selectOne(
                    new EntityWrapper<EntpLocEntity>().eq("entp_id", shipmentEntity.getOriginId()).ne("type", 3));

            EntpLocEntity routeEntity = entpLocService.selectOne(
                    new EntityWrapper<EntpLocEntity>().eq("entp_id", shipmentEntity.getCustomerId()).eq("type", 3).setSqlSelect("lnglat"));

            return R.ok().put("carrier", carrierLocEntity).put("customer", customerLocEntity).put("route", routeEntity).put("load", loadLocEntity);
        }else{
            return R.ok();
        }

    }

    /**
     * 查询某家企业的线路信息
     */
    @RequestMapping("/queryRouteByEntpId/{entpId}")
    // @RequiresPermissions("bussiness:lockstatus:info")
    public R queryRouteByEntpId(@PathVariable("entpId") Long entpId) {
        EntpLocEntity entpLocEntity = entpLocService.selectOne(
                new EntityWrapper<EntpLocEntity>().eq("entp_id", entpId).eq("type",3));
        return R.ok().put("data", entpLocEntity);
    }


    /**
     * 查询某家企业的围栏信息
     */
    @RequestMapping("/queryFenceByEntpId/{entpId}")
    // @RequiresPermissions("bussiness:lockstatus:info")
    public R queryFenceByEntpId(@PathVariable("entpId") Long entpId) {
        EntpLocEntity entpLocEntity = entpLocService.selectOne(
                new EntityWrapper<EntpLocEntity>().eq("entp_id", entpId).ne("type",3));
        return R.ok().put("data", entpLocEntity);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("bussiness:entploc:save")
    public R save(@RequestBody EntpLocEntity entpLoc) {
        entpLocService.insert(entpLoc);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("bussiness:entploc:update")
    public R update(@RequestBody EntpLocEntity entpLoc) {
        entpLocService.updateById(entpLoc);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("bussiness:entploc:delete")
    public R delete(@RequestBody Long[] ids) {
        entpLocService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }


}
